<template>
  <div class="app-container">
    <div class="app-flex">
      <el-input
        placeholder="请输入姓名进行查询"
        v-model="searchByName"
        class="input-with-select"
        style="width: 400px; margin: 10px"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="clickSearchButton"
        ></el-button>
      </el-input>
    </div>
    <el-table
      v-loading="listLoading"
      :data="userPageData.records"
      border
      style="width: 100%"
    >
      <el-table-column fixed="left" label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleUpdateShow(scope.row)"
            >维护</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="姓名">
        <template slot-scope="scope">
          {{ getNameFromId(scope.row.id) }}
        </template>
      </el-table-column>
      <el-table-column label="资信等级">
        <template slot-scope="scope">
          <el-rate
            :value="scope.row.levels && Math.ceil(scope.row.levels / 20)"
            disabled
            :max="5"
          >
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column prop="ranges" label="贷款额度(￥)" />
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, sizes, total, jumper"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="userPageData.size"
      :total="userPageData.total"
      @current-change="fetchData"
      @size-change="handleSizeChange"
    />

    <el-dialog title="资信反馈" :visible.sync="dialogFormVisible">
      <el-form
        ref="UserForm"
        :rules="rules"
        :model="formData"
        label-width="100px"
      >
        <el-form-item label="资信等级" prop="levels">
          <el-input type="number" v-model="formData.levels" />
        </el-form-item>
        <!-- <el-form-item label="贷款额度(￥)">
          <el-input v-model="formData.ranges" />
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="saveFormData">保存</el-button>
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getList, saveData, deleteUser } from "@/api/biz/feedback";
import { getTargetKeyValueFromSource, getDict } from "@/utils/easyUtils";

export default {
  data() {
    return {
      dialogFormVisible: false,
      listLoading: true,
      userPageData: {
        records: [],
        size: 10,
        total: 0,
        current: 1,
      },
      formData: {
        id: 0,
        levels: "",
        ranges: "",
      },
      searchByName: "",
      dict: [],
      rules: {
        levels: [
          { required: true, message: "请填入资信等级", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.fetchData(1);
    getDict().then((res) => {
      this.dict = res.data;
    });
  },
  methods: {
    clickSearchButton() {
      // console.log(this.searchByName);
      getList({})
        .then((response) => {
          var obj = response.data.records;
          for (const key in obj) {
            if (this.getNameFromId(obj[key].id) === this.searchByName) {
              response.data.records = [obj[key]];
              this.userPageData = getTargetKeyValueFromSource(
                this.userPageData,
                response.data
              );
              return;
            }
          }
          this.$message.warning("查无此人！");
          return;
        })
        .catch((err) => {
          this.listLoading = false;
          console.log(err);
        });
    },
    getNameFromId(id) {
      let dict = this.dict;
      for (let index = 0; index < dict.length; index++) {
        const element = dict[index];
        if (element.id === id) {
          return element.uname;
        }
      }
    },
    handleSizeChange(val) {
      this.userPageData.size = val;
      this.fetchData(1);
    },
    fetchData(current) {
      this.userPageData.current = current;
      this.userPageData.records = [];
      this.listLoading = true;
      getList({}, this.userPageData)
        .then((response) => {
          this.userPageData = getTargetKeyValueFromSource(
            this.userPageData,
            response.data
          );
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
          console.log(err);
        });
    },
    handleUpdateShow(row) {
      this.formData = getTargetKeyValueFromSource(this.formData, row);
      this.dialogFormVisible = true;
    },
    handleDelete(row) {
      this.$confirm("确定删除用户？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteUser({ userId: row.id }).then((response) => {
            this.fetchData(1);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    saveFormData() {
      this.$refs['UserForm'].validate((valid) => {
        if (valid) {
          saveData(this.formData).then((response) => {
            this.dialogFormVisible = false;
            this.fetchData(1);
          });
        } else {
          this.$message.warning("输入数据错误！");
        }
      });
    },
    addData() {
      this.formData = getTargetKeyValueFromSource(this.formData, {
        id: 0,
        levels: "",
        ranges: "",
      });
      this.dialogFormVisible = true;
    },
  },
};
</script>

<style scoped>
</style>